package com.zmn.oms.third.pinduoduo.service.impl;

import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.common.constant.OrderLogConsts;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.model.entity.work.OrderWorkLogChange;
import com.zmn.oms.third.pinduoduo.PinDuoDuoConsts;
import com.zmn.oms.third.pinduoduo.service.PinDuoDuoService;
import com.zmn.oms.zmn.business.interfaces.open.OrderChangeNotifyService;
import com.zmn.oms.zmn.business.interfaces.open.OrderNewNotifyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 类描述：拼多多推送订单变化的服务
 *
 * @author wangxiaokun
 * @since 2020/5/31 15:19
 */
@Service("pinDuoDuoNotifyService")
@Slf4j
public class PinDuoDuoNotifyServiceImpl implements OrderChangeNotifyService, OrderNewNotifyService {

    @Autowired
    private PinDuoDuoService pinDuoDuoService;

    /**
     * 根据订单状态变化推送到拼多多平台
     * @param orderWorkLogChange 
     * @return void
     * @author wangxiaokun
     */
    @Override
    public void notifyChange(OrderWorkLogChange orderWorkLogChange) {
        // 判断功能是否开启
        if (!PinDuoDuoConsts.POWER_ON) {
            log.info("【{}】<订单状态同步> 该功能没有开启。", PinDuoDuoConsts.CHANNEL_NAME);
            return;
        }

        log.info("【{}】<订单状态同步> , 状态信息:[{}]。", PinDuoDuoConsts.CHANNEL_NAME, orderWorkLogChange.toString());

        int logType = orderWorkLogChange.getLogType().intValue();

        try{
            switch (logType) {
                // 服务预约
                case OrderLogConsts.ORDER_LOG_TYPE_UPDATE:
                case OrderLogConsts.ORDER_LOG_TYPE_MASTER_MODIFY_DUTY_TIME:
                    pinDuoDuoService.sendAppointOrderToPinDuoDuo(orderWorkLogChange);
                    break;
                // 派单
                case OrderLogConsts.ORDER_LOG_TYPE_DISTRIBUTE:
                    pinDuoDuoService.sendUpdateOrderToPinDuoDuo(orderWorkLogChange);
                    break;
                // 工程师上门
                case OrderLogConsts.ORDER_LOG_TYPE_VISIT:
                    pinDuoDuoService.sendBeginServiceToPinDuoDuo(orderWorkLogChange);
                    break;
                // 取消、失败、弃单
                case OrderLogConsts.ORDER_LOG_TYPE_CANCEL:
                case OrderConsts.ORDER_OP_TYPE_FAIL:
                case OrderConsts.ORDER_OP_TYPE_DISCARD:
                    pinDuoDuoService.sendCancelOrderToPinDuoDuo(orderWorkLogChange);
                    break;
                // 服务完成
                case OrderLogConsts.ORDER_LOG_TYPE_SERVICE_COMPLETE:
                    pinDuoDuoService.sendFinishOrderToPinDuoDuo(orderWorkLogChange);
                    break;
                default:
                    return;
            }
        } catch (OmsBaseException e) {
            log.error("【{}】<订单状态同步>失败，订单状态为:[{}]，提示消息:[{}]。", PinDuoDuoConsts.CHANNEL_NAME, logType, e.getMessage());
        }

        log.info("【{}】<订单状态同步完成> 。", PinDuoDuoConsts.CHANNEL_NAME);
    }

    /**
     * 新单添加成功推送到拼多多平台
     * @param orderWorkLogChange 
     * @return void
     * @author wangxiaokun
     */
    @Override
    public void notifyNewOrder(OrderWorkLogChange orderWorkLogChange) throws OmsBaseException {

        // 判断功能是否开启
        if (!PinDuoDuoConsts.POWER_ON) {
            log.info("【{}】<订单状态同步-新订单> 该功能没有开启。", PinDuoDuoConsts.CHANNEL_NAME);
            return;
        }

        // 同步接单消息给拼多多平台
        try {
            log.info("【{}】<订单状态同步-新订单>, 订单状态:[{}] ", PinDuoDuoConsts.CHANNEL_NAME, orderWorkLogChange.toString());
            pinDuoDuoService.sendAcceptOrderToPinDuoDuo(orderWorkLogChange);
        } catch (OmsBaseException e) {
            log.error("【{}】<订单状态同步-新订单>失败:[{}]。", PinDuoDuoConsts.CHANNEL_NAME, e.getMessage());
        }
    }
}
